<?php
/**
 * @title 365. 水壶问题 - 广度遍历
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer $jug1Capacity
     * @param Integer $jug2Capacity
     * @param Integer $targetCapacity
     * @return Boolean
     */
    function canMeasureWater($jug1Capacity, $jug2Capacity, $targetCapacity) {
        /**
         * @since 2024-01-28 遍历过的点
         */
        $visited = [
            "{$jug1Capacity}-{$jug2Capacity}" => true,
        ];

        /**
         * @since 2024-01-28 BFS广度遍历
         */
        $nodeArray = [[$jug1Capacity, $jug2Capacity]];
        while($nodeArray){
            /**
             * @since 2024-01-28 下次遍历元素的集合
             */
            $nextNodeArray = [];

            /**
             * @since 2024-01-28 遍历当前元素集合
             */
            foreach ($nodeArray as list($a, $b)){
                /**
                 * @since 2024-01-28 是否目标值
                 */
                if($a == $targetCapacity OR $b == $targetCapacity OR $a+$b == $targetCapacity){
                    return true;
                } else {}

                /**
                 * @since 2024-01-29 操作一次后可能的情况
                 */
                $nextArray = [];
                $nextArray[] = [0, $b]; // a倒空
                $nextArray[] = [$jug1Capacity, $b]; // a装满
                /**
                 * @since 2024-01-28 a倒给b
                 */
                if($a+$b>$jug2Capacity){
                    $nextArray[] = [$a+$b-$jug2Capacity, $jug2Capacity];
                } else {
                    $nextArray[] = [0, $a+$b];
                }

                $nextArray[] = [$a, 0];
                $nextArray[] = [$a, $jug2Capacity];
                if($a+$b>$jug1Capacity){
                    $nextArray[] = [$jug1Capacity, $a+$b-$jug1Capacity];
                } else {
                    $nextArray[] = [$a+$b, 0];
                }

                /**
                 * @since 2024-01-28 下次遍历的元素
                 */
                foreach ($nextArray as list($nextA, $nextB)){
                    if(!isset($visited["{$nextA}-{$nextB}"])){
                        $visited["{$nextA}-{$nextB}"] = true;
                        $nextNodeArray[] = [$nextA, $nextB];
                    } else {}
                }
            }

            /**
             * @since 2024-01-28 下次遍历
             */
            $nodeArray = $nextNodeArray;
        }

        /**
         * @return Boolean
         */
        return false;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.28.php
 */
$datas = [
    [3,5,4], // true
    [2,6,5], // false
    [1,2,3], // true
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->canMeasureWater(...$data);
    echo $xhprof->DumpArgs([$result]);
    var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
